//
//  DetailViewController.m
//  zhongwulegou
//
//  Created by qianfeng on 15/9/28.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "DetailViewController.h"
#import "DetailHeadCell.h"
#import "DetailCell1.h"
#import "DetailCell2.h"
#import "DetailCell3.h"
#import "DetailCell4.h"
#import "DetailCell5.h"
#import "DetailCell6.h"
#import "HttpRequestManager.h"


@interface DetailViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSDictionary *_goodsDetailDic;
}

@property (weak, nonatomic) IBOutlet UITableView *myTableView;

@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    
    [_myTableView registerNib:[UINib nibWithNibName:@"DetailHeadCell" bundle:nil] forCellReuseIdentifier:@"DetailHeadCell"];
    [_myTableView registerNib:[UINib nibWithNibName:@"DetailCell1" bundle:nil] forCellReuseIdentifier:@"DetailCell1"];
    [_myTableView registerNib:[UINib nibWithNibName:@"DetailCell2" bundle:nil] forCellReuseIdentifier:@"DetailCell2"];
    [_myTableView registerNib:[UINib nibWithNibName:@"DetailCell3" bundle:nil] forCellReuseIdentifier:@"DetailCell3"];
    [_myTableView registerNib:[UINib nibWithNibName:@"DetailCell4" bundle:nil] forCellReuseIdentifier:@"DetailCell4"];
    [_myTableView registerNib:[UINib nibWithNibName:@"DetailCell5" bundle:nil] forCellReuseIdentifier:@"DetailCell5"];
    [_myTableView registerNib:[UINib nibWithNibName:@"DetailCell6" bundle:nil] forCellReuseIdentifier:@"DetailCell6"];
    
    NSLog(@"%@",self.goodsID);
    
    [self LoadDataFromNet];
}

-(void)LoadDataFromNet
{
    [[HttpRequestManager shareInstance] getDetailInfoWithGoodsID:self.goodsID success:^(id responseObj) {
        NSDictionary *datasDic = responseObj;
        NSString *imageString = datasDic[@"goods_image"];
        NSArray *imageArray = [imageString 	componentsSeparatedByString:@","];
        //滚动图数组
        [_goodsDetailDic setValue:imageArray forKey:@"imageArray"];
        NSDictionary *goodsInfoDic = datasDic[@"goods_info"];

        [_goodsDetailDic setValue:goodsInfoDic[@"goods_name"] forKey:@"name"];
        [_goodsDetailDic setValue:goodsInfoDic[@"goods_marketprice"] forKey:@"marketPrice"];
        [_goodsDetailDic setValue:goodsInfoDic[@"promotion_price"] forKey:@"nowPrice"];
        [_goodsDetailDic setValue:goodsInfoDic[@"goods_salenum"] forKey:@"saleNumber"];
        
        NSArray *commentArray = datasDic[@"goodsevallist"];
        //评论数组
        [_goodsDetailDic setValue:commentArray forKey:@"commentArray"];
        //同类品牌
        [_goodsDetailDic setValue:goodsInfoDic[@"brand_name"] forKey:@"brandName"];
        
        NSDictionary *goodsDetailDic = datasDic[@"goods_detail"];
        NSArray *attrArray = goodsDetailDic[@"goods_attr"];
        
        //规格参数数组
        [_goodsDetailDic setValue:attrArray forKey:@"attrArray"];
        
        [_myTableView reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"获取详情页数据失败,error==%@",error);
    }];
}

- (IBAction)back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/



#pragma mark -UITableViewDelegate&UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else if(section == 1){
        return 5;
    }else{
        return 1;
    }
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        DetailHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailHeadCell"];
        NSArray *imageArray = _goodsDetailDic[@"imageArray"];
        cell.headScrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.width);
        
        
        return cell;
    }else if(indexPath.section == 1){
        if (indexPath.row == 0) {
            DetailCell1 *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailCell1"];
            
            return cell;
        }else if (indexPath.row == 1){
            DetailCell2 *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailCell2"];
            return cell;
        }else if (indexPath.row == 2){
            DetailCell3 *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailCell3"];
            return cell;
        }else if (indexPath.row == 3){
            DetailCell4 *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailCell4"];
            return cell;
        }else{
            DetailCell5 *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailCell5"];
            return cell;
        }
    
    }else{

        DetailCell6 *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailCell6"];
        return cell;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return _myTableView.bounds.size.width+60;
    }else if(indexPath.section == 1){
        if (indexPath.row == 0) {
            return 30;
        }else if (indexPath.row == 1){
            return 44;
        }else if (indexPath.row == 2){
            return 64;
        }else if (indexPath.row == 3){
            return 44;
        }else{
            return 64;
        }
        
    }else{
        return 30;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}



@end
